package com.fl.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.fl.serivce.MailService;
import com.fl.sys.SysConfig;
import com.fl.util.DbUtil;

/**
 * PARAM表所有操作
 * 
 * @author liliang
 * 
 */
public class ParamDao {

	private static Logger log = Logger.getLogger(MailService.class);

	public static String getParamTableVal(String key) throws Exception {

		String sql = "SELECT " + SysConfig.TALBE_COL_VAL + " FROM "
				+ SysConfig.TALBE_PARAM + " WHERE " + SysConfig.TALBE_COL_KEY
				+ " =? ";
		List<Object> param = new ArrayList<Object>();
		param.add(key);
		List<Map<String, Object>> results = DbUtil.query(sql, param);
		if (results == null || results.size() == 0) {
			return null;
		}

		Object value = results.get(0)
				.get(SysConfig.TALBE_COL_VAL.toUpperCase());

		if (value == null) {
			return "";
		} else {
			return value.toString().trim();
		}

	}

	public static void updateParamValue(String key, String value)
			throws Exception {
		// 如果不存在则插入数据，如果存在则更新数据
		String sql = "SELECT " + SysConfig.TALBE_COL_VAL + " FROM "
				+ SysConfig.TALBE_PARAM + " WHERE " + SysConfig.TALBE_COL_KEY
				+ " =? ";
		List<Object> param = new ArrayList<Object>();
		param.add(key);
		List<Map<String, Object>> results = DbUtil.query(sql, param);
		if (results != null && results.size() > 0) {
			// 已经存在，做更新操作
			sql = "UPDATE " + SysConfig.TALBE_PARAM + " SET "
					+ SysConfig.TALBE_COL_VAL + "=? WHERE "
					+ SysConfig.TALBE_COL_KEY + " =? ";
			param = new ArrayList<Object>();
			param.add(value);
			param.add(key);
			DbUtil.exex(sql, param);
		} else {
			// 不存在，做插入操作
			sql = "INSERT INTO " + SysConfig.TALBE_PARAM + " VALUES(?,?) ";
			param = new ArrayList<Object>();
			param.add(key);
			param.add(value);
			DbUtil.exex(sql, param);
		}

	}

}
